<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pool.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_pool.js.html">pool.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Datastructure: Pool.
<a name="line17"></a> *
<a name="line18"></a> *
<a name="line19"></a> * A generic class for handling pools of objects.
<a name="line20"></a> * When an object is released, it is attempted to be reused.
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.structs.Pool&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line27"></a>goog.require(&#39;goog.structs.Queue&#39;);
<a name="line28"></a>goog.require(&#39;goog.structs.Set&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * A generic pool class. If min is greater than max, an error is thrown.
<a name="line34"></a> * @param {number=} opt_minCount Min. number of objects (Default: 1).
<a name="line35"></a> * @param {number=} opt_maxCount Max. number of objects (Default: 10).
<a name="line36"></a> * @constructor
<a name="line37"></a> * @extends {goog.Disposable}
<a name="line38"></a> * @template T
<a name="line39"></a> */
<a name="line40"></a>goog.structs.Pool = function(opt_minCount, opt_maxCount) {
<a name="line41"></a>  goog.Disposable.call(this);
<a name="line42"></a>
<a name="line43"></a>  /**
<a name="line44"></a>   * Minimum number of objects allowed
<a name="line45"></a>   * @private {number}
<a name="line46"></a>   */
<a name="line47"></a>  this.minCount_ = opt_minCount || 0;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * Maximum number of objects allowed
<a name="line51"></a>   * @private {number}
<a name="line52"></a>   */
<a name="line53"></a>  this.maxCount_ = opt_maxCount || 10;
<a name="line54"></a>
<a name="line55"></a>  // Make sure that the max and min constraints are valid.
<a name="line56"></a>  if (this.minCount_ &gt; this.maxCount_) {
<a name="line57"></a>    throw Error(goog.structs.Pool.ERROR_MIN_MAX_);
<a name="line58"></a>  }
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * Set used to store objects that are currently in the pool and available
<a name="line62"></a>   * to be used.
<a name="line63"></a>   * @private {goog.structs.Queue.&lt;T&gt;}
<a name="line64"></a>   */
<a name="line65"></a>  this.freeQueue_ = new goog.structs.Queue();
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * Set used to store objects that are currently in the pool and in use.
<a name="line69"></a>   * @private {goog.structs.Set.&lt;T&gt;}
<a name="line70"></a>   */
<a name="line71"></a>  this.inUseSet_ = new goog.structs.Set();
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * The minimum delay between objects being made available, in milliseconds. If
<a name="line75"></a>   * this is 0, no minimum delay is enforced.
<a name="line76"></a>   * @protected {number}
<a name="line77"></a>   */
<a name="line78"></a>  this.delay = 0;
<a name="line79"></a>
<a name="line80"></a>  /**
<a name="line81"></a>   * The time of the last object being made available, in milliseconds since the
<a name="line82"></a>   * epoch (i.e., the result of Date#toTime). If this is null, no access has
<a name="line83"></a>   * occurred yet.
<a name="line84"></a>   * @protected {number?}
<a name="line85"></a>   */
<a name="line86"></a>  this.lastAccess = null;
<a name="line87"></a>
<a name="line88"></a>  // Make sure that the minCount constraint is satisfied.
<a name="line89"></a>  this.adjustForMinMax();
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>  // TODO(user): Remove once JSCompiler&#39;s undefined properties warnings
<a name="line93"></a>  // don&#39;t error for guarded properties.
<a name="line94"></a>  var magicProps = {canBeReused: 0};
<a name="line95"></a>};
<a name="line96"></a>goog.inherits(goog.structs.Pool, goog.Disposable);
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Error to throw when the max/min constraint is attempted to be invalidated.
<a name="line101"></a> * I.e., when it is attempted for maxCount to be less than minCount.
<a name="line102"></a> * @type {string}
<a name="line103"></a> * @private
<a name="line104"></a> */
<a name="line105"></a>goog.structs.Pool.ERROR_MIN_MAX_ =
<a name="line106"></a>    &#39;[goog.structs.Pool] Min can not be greater than max&#39;;
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Error to throw when the Pool is attempted to be disposed and it is asked to
<a name="line111"></a> * make sure that there are no objects that are in use (i.e., haven&#39;t been
<a name="line112"></a> * released).
<a name="line113"></a> * @type {string}
<a name="line114"></a> * @private
<a name="line115"></a> */
<a name="line116"></a>goog.structs.Pool.ERROR_DISPOSE_UNRELEASED_OBJS_ =
<a name="line117"></a>    &#39;[goog.structs.Pool] Objects not released&#39;;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Sets the minimum count of the pool.
<a name="line122"></a> * If min is greater than the max count of the pool, an error is thrown.
<a name="line123"></a> * @param {number} min The minimum count of the pool.
<a name="line124"></a> */
<a name="line125"></a>goog.structs.Pool.prototype.setMinimumCount = function(min) {
<a name="line126"></a>  // Check count constraints.
<a name="line127"></a>  if (min &gt; this.maxCount_) {
<a name="line128"></a>    throw Error(goog.structs.Pool.ERROR_MIN_MAX_);
<a name="line129"></a>  }
<a name="line130"></a>  this.minCount_ = min;
<a name="line131"></a>
<a name="line132"></a>  // Adjust the objects in the pool as needed.
<a name="line133"></a>  this.adjustForMinMax();
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Sets the maximum count of the pool.
<a name="line139"></a> * If max is less than the max count of the pool, an error is thrown.
<a name="line140"></a> * @param {number} max The maximum count of the pool.
<a name="line141"></a> */
<a name="line142"></a>goog.structs.Pool.prototype.setMaximumCount = function(max) {
<a name="line143"></a>  // Check count constraints.
<a name="line144"></a>  if (max &lt; this.minCount_) {
<a name="line145"></a>    throw Error(goog.structs.Pool.ERROR_MIN_MAX_);
<a name="line146"></a>  }
<a name="line147"></a>  this.maxCount_ = max;
<a name="line148"></a>
<a name="line149"></a>  // Adjust the objects in the pool as needed.
<a name="line150"></a>  this.adjustForMinMax();
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Sets the minimum delay between objects being returned by getObject, in
<a name="line156"></a> * milliseconds. This defaults to zero, meaning that no minimum delay is
<a name="line157"></a> * enforced and objects may be used as soon as they&#39;re available.
<a name="line158"></a> * @param {number} delay The minimum delay, in milliseconds.
<a name="line159"></a> */
<a name="line160"></a>goog.structs.Pool.prototype.setDelay = function(delay) {
<a name="line161"></a>  this.delay = delay;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * @return {T|undefined} A new object from the pool if there is one available,
<a name="line167"></a> *     otherwise undefined.
<a name="line168"></a> */
<a name="line169"></a>goog.structs.Pool.prototype.getObject = function() {
<a name="line170"></a>  var time = goog.now();
<a name="line171"></a>  if (goog.isDefAndNotNull(this.lastAccess) &amp;&amp;
<a name="line172"></a>      time - this.lastAccess &lt; this.delay) {
<a name="line173"></a>    return undefined;
<a name="line174"></a>  }
<a name="line175"></a>
<a name="line176"></a>  var obj = this.removeFreeObject_();
<a name="line177"></a>  if (obj) {
<a name="line178"></a>    this.lastAccess = time;
<a name="line179"></a>    this.inUseSet_.add(obj);
<a name="line180"></a>  }
<a name="line181"></a>
<a name="line182"></a>  return obj;
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Returns an object to the pool of available objects so that it can be reused.
<a name="line188"></a> * @param {T} obj The object to return to the pool of free objects.
<a name="line189"></a> * @return {boolean} Whether the object was found in the Pool&#39;s set of in-use
<a name="line190"></a> *     objects (in other words, whether any action was taken).
<a name="line191"></a> */
<a name="line192"></a>goog.structs.Pool.prototype.releaseObject = function(obj) {
<a name="line193"></a>  if (this.inUseSet_.remove(obj)) {
<a name="line194"></a>    this.addFreeObject(obj);
<a name="line195"></a>    return true;
<a name="line196"></a>  }
<a name="line197"></a>  return false;
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Removes a free object from the collection of objects that are free so that it
<a name="line203"></a> * can be used.
<a name="line204"></a> *
<a name="line205"></a> * NOTE: This method does not mark the returned object as in use.
<a name="line206"></a> *
<a name="line207"></a> * @return {T|undefined} The object removed from the free collection, if there
<a name="line208"></a> *     is one available. Otherwise, undefined.
<a name="line209"></a> * @private
<a name="line210"></a> */
<a name="line211"></a>goog.structs.Pool.prototype.removeFreeObject_ = function() {
<a name="line212"></a>  var obj;
<a name="line213"></a>  while (this.getFreeCount() &gt; 0) {
<a name="line214"></a>    obj = this.freeQueue_.dequeue();
<a name="line215"></a>
<a name="line216"></a>    if (!this.objectCanBeReused(obj)) {
<a name="line217"></a>      this.adjustForMinMax();
<a name="line218"></a>    } else {
<a name="line219"></a>      break;
<a name="line220"></a>    }
<a name="line221"></a>  }
<a name="line222"></a>
<a name="line223"></a>  if (!obj &amp;&amp; this.getCount() &lt; this.maxCount_) {
<a name="line224"></a>    obj = this.createObject();
<a name="line225"></a>  }
<a name="line226"></a>
<a name="line227"></a>  return obj;
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Adds an object to the collection of objects that are free. If the object can
<a name="line233"></a> * not be added, then it is disposed.
<a name="line234"></a> *
<a name="line235"></a> * @param {T} obj The object to add to collection of free objects.
<a name="line236"></a> */
<a name="line237"></a>goog.structs.Pool.prototype.addFreeObject = function(obj) {
<a name="line238"></a>  this.inUseSet_.remove(obj);
<a name="line239"></a>  if (this.objectCanBeReused(obj) &amp;&amp; this.getCount() &lt; this.maxCount_) {
<a name="line240"></a>    this.freeQueue_.enqueue(obj);
<a name="line241"></a>  } else {
<a name="line242"></a>    this.disposeObject(obj);
<a name="line243"></a>  }
<a name="line244"></a>};
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>/**
<a name="line248"></a> * Adjusts the objects held in the pool to be within the min/max constraints.
<a name="line249"></a> *
<a name="line250"></a> * NOTE: It is possible that the number of objects in the pool will still be
<a name="line251"></a> * greater than the maximum count of objects allowed. This will be the case
<a name="line252"></a> * if no more free objects can be disposed of to get below the minimum count
<a name="line253"></a> * (i.e., all objects are in use).
<a name="line254"></a> */
<a name="line255"></a>goog.structs.Pool.prototype.adjustForMinMax = function() {
<a name="line256"></a>  var freeQueue = this.freeQueue_;
<a name="line257"></a>
<a name="line258"></a>  // Make sure the at least the minimum number of objects are created.
<a name="line259"></a>  while (this.getCount() &lt; this.minCount_) {
<a name="line260"></a>    freeQueue.enqueue(this.createObject());
<a name="line261"></a>  }
<a name="line262"></a>
<a name="line263"></a>  // Make sure no more than the maximum number of objects are created.
<a name="line264"></a>  while (this.getCount() &gt; this.maxCount_ &amp;&amp; this.getFreeCount() &gt; 0) {
<a name="line265"></a>    this.disposeObject(freeQueue.dequeue());
<a name="line266"></a>  }
<a name="line267"></a>};
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>/**
<a name="line271"></a> * Should be overridden by sub-classes to return an instance of the object type
<a name="line272"></a> * that is expected in the pool.
<a name="line273"></a> * @return {T} The created object.
<a name="line274"></a> */
<a name="line275"></a>goog.structs.Pool.prototype.createObject = function() {
<a name="line276"></a>  return {};
<a name="line277"></a>};
<a name="line278"></a>
<a name="line279"></a>
<a name="line280"></a>/**
<a name="line281"></a> * Should be overridden to dispose of an object. Default implementation is to
<a name="line282"></a> * remove all its members, which should render it useless. Calls the object&#39;s
<a name="line283"></a> * {@code dispose()} method, if available.
<a name="line284"></a> * @param {T} obj The object to dispose.
<a name="line285"></a> */
<a name="line286"></a>goog.structs.Pool.prototype.disposeObject = function(obj) {
<a name="line287"></a>  if (typeof obj.dispose == &#39;function&#39;) {
<a name="line288"></a>    obj.dispose();
<a name="line289"></a>  } else {
<a name="line290"></a>    for (var i in obj) {
<a name="line291"></a>      obj[i] = null;
<a name="line292"></a>    }
<a name="line293"></a>  }
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Should be overridden to determine whether an object has become unusable and
<a name="line299"></a> * should not be returned by getObject(). Calls the object&#39;s
<a name="line300"></a> * {@code canBeReused()}  method, if available.
<a name="line301"></a> * @param {T} obj The object to test.
<a name="line302"></a> * @return {boolean} Whether the object can be reused.
<a name="line303"></a> */
<a name="line304"></a>goog.structs.Pool.prototype.objectCanBeReused = function(obj) {
<a name="line305"></a>  if (typeof obj.canBeReused == &#39;function&#39;) {
<a name="line306"></a>    return obj.canBeReused();
<a name="line307"></a>  }
<a name="line308"></a>  return true;
<a name="line309"></a>};
<a name="line310"></a>
<a name="line311"></a>
<a name="line312"></a>/**
<a name="line313"></a> * Returns true if the given object is in the pool.
<a name="line314"></a> * @param {T} obj The object to check the pool for.
<a name="line315"></a> * @return {boolean} Whether the pool contains the object.
<a name="line316"></a> */
<a name="line317"></a>goog.structs.Pool.prototype.contains = function(obj) {
<a name="line318"></a>  return this.freeQueue_.contains(obj) || this.inUseSet_.contains(obj);
<a name="line319"></a>};
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>/**
<a name="line323"></a> * Returns the number of objects currently in the pool.
<a name="line324"></a> * @return {number} Number of objects currently in the pool.
<a name="line325"></a> */
<a name="line326"></a>goog.structs.Pool.prototype.getCount = function() {
<a name="line327"></a>  return this.freeQueue_.getCount() + this.inUseSet_.getCount();
<a name="line328"></a>};
<a name="line329"></a>
<a name="line330"></a>
<a name="line331"></a>/**
<a name="line332"></a> * Returns the number of objects currently in use in the pool.
<a name="line333"></a> * @return {number} Number of objects currently in use in the pool.
<a name="line334"></a> */
<a name="line335"></a>goog.structs.Pool.prototype.getInUseCount = function() {
<a name="line336"></a>  return this.inUseSet_.getCount();
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/**
<a name="line341"></a> * Returns the number of objects currently free in the pool.
<a name="line342"></a> * @return {number} Number of objects currently free in the pool.
<a name="line343"></a> */
<a name="line344"></a>goog.structs.Pool.prototype.getFreeCount = function() {
<a name="line345"></a>  return this.freeQueue_.getCount();
<a name="line346"></a>};
<a name="line347"></a>
<a name="line348"></a>
<a name="line349"></a>/**
<a name="line350"></a> * Determines if the pool contains no objects.
<a name="line351"></a> * @return {boolean} Whether the pool contains no objects.
<a name="line352"></a> */
<a name="line353"></a>goog.structs.Pool.prototype.isEmpty = function() {
<a name="line354"></a>  return this.freeQueue_.isEmpty() &amp;&amp; this.inUseSet_.isEmpty();
<a name="line355"></a>};
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/**
<a name="line359"></a> * Disposes of the pool and all objects currently held in the pool.
<a name="line360"></a> * @override
<a name="line361"></a> * @protected
<a name="line362"></a> */
<a name="line363"></a>goog.structs.Pool.prototype.disposeInternal = function() {
<a name="line364"></a>  goog.structs.Pool.superClass_.disposeInternal.call(this);
<a name="line365"></a>  if (this.getInUseCount() &gt; 0) {
<a name="line366"></a>    throw Error(goog.structs.Pool.ERROR_DISPOSE_UNRELEASED_OBJS_);
<a name="line367"></a>  }
<a name="line368"></a>  delete this.inUseSet_;
<a name="line369"></a>
<a name="line370"></a>  // Call disposeObject on each object held by the pool.
<a name="line371"></a>  var freeQueue = this.freeQueue_;
<a name="line372"></a>  while (!freeQueue.isEmpty()) {
<a name="line373"></a>    this.disposeObject(freeQueue.dequeue());
<a name="line374"></a>  }
<a name="line375"></a>  delete this.freeQueue_;
<a name="line376"></a>};
</pre>


</body>
</html>
